﻿/**
 * My Pomodoro Watch
 * Rather than buying/downloading an application, I thought it better to write my own. An hour later, here is the product of the work.
 * Original Code - Binu John
 * 1st June 2012.
 * TODO: 1. Putting in an actual ticking watch.
 *       2. UI Modifications.
 *       3. Minimizing the application to the task bar.
 *       4. Adding right click menu on the taskbar icon.
**/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Pomodoro_Watch
{
    public partial class frmPomodoroWatch : Form
    {
        public frmPomodoroWatch()
        {
            InitializeComponent();
        }
        private int intPauseTime = 0;
        private int intWorkTime = 0;
        private int intIterations = 0;
        private int counter = 0;
        private void btnStartTimer_Click(object sender, EventArgs e)
        {
            try //To catch any blank textboxes or text values entered in the textboxes
            {
                intWorkTime = Convert.ToInt32(txtWorkTime.Text);// *1000 * 60; //To convert minutes in the text box to milliseconds that timer understands.
                intPauseTime = Convert.ToInt32(txtPauseTime.Text);// *1000 * 60;
                intIterations = Convert.ToInt32(txtIterations.Text);//This is the number of work units after which the user takes a longer break.
            }
            catch (System.FormatException fe)
            {
                MessageBox.Show("Please enter number of minutes in the textboxes.");
                return;
            }
            counter = 0;
            tmrPomodoroTimer.Interval = intWorkTime;
            tmrPomodoroTimer.Tick += new EventHandler(tmrPomodoroTimer_Work);
            tmrPomodoroTimer.Enabled = true;
            btnStartTimer.Enabled = false; //Toggling the two buttons
            btnStopTimer.Enabled = true;
        }

        private void tmrPomodoroTimer_Work(object sender, EventArgs e)
        {

            tmrPomodoroTimer.Enabled = false;
            frmAlert f = new frmAlert("Work Time Out. Take a break!", "Take a break");
            f.ShowDialog();
            //MessageBox.Show("Work Time Out. Take a break!"); //Modal dialog box rather than the plain old message box
            tmrPomodoroTimer.Interval = intPauseTime;
            tmrPomodoroTimer.Tick -= tmrPomodoroTimer_Work;
            tmrPomodoroTimer.Tick += new EventHandler(tmrPomodoroTimer_Pause);
            tmrPomodoroTimer.Enabled = true;
            counter++;

            if (counter >= intIterations) //If the number of iterations are done, then the user takes a longer break.
            {
               tmrPomodoroTimer.Enabled = false;
               tmrPomodoroTimer.Tick -= tmrPomodoroTimer_Pause;
               tmrPomodoroTimer.Tick -= tmrPomodoroTimer_Work;
               btnStopTimer.Enabled = false;
               btnStartTimer.Enabled = true;
               f = new frmAlert("Take a longer break now.", "Take a break");
               f.ShowDialog();
            }
        }
        private void tmrPomodoroTimer_Pause(object sender, EventArgs e)
        {
            tmrPomodoroTimer.Enabled = false;
            //MessageBox.Show("Back to work buddy!");
            frmAlert f = new frmAlert("Back to work buddy!", "Stop procrastinating!"); //Modal dialog box rather than the plain old message box
            f.ShowDialog();
            tmrPomodoroTimer.Interval = intWorkTime;
            tmrPomodoroTimer.Tick -= tmrPomodoroTimer_Pause;
            tmrPomodoroTimer.Tick += new EventHandler(tmrPomodoroTimer_Work);
            tmrPomodoroTimer.Enabled = true;
        }

        private void btnStopTimer_Click(object sender, EventArgs e)
        {
            tmrPomodoroTimer.Enabled = false;
            tmrPomodoroTimer.Tick -= tmrPomodoroTimer_Pause; //Toggling the buttons back
            tmrPomodoroTimer.Tick -= tmrPomodoroTimer_Work;
            btnStopTimer.Enabled = false;
            btnStartTimer.Enabled = true;
        }
    }
}
